package main

import "fmt"
import "time"
import "math/rand"

func main() {
	rand.Seed(time.Now().UnixNano())
	var num int
	var slice [4]int
	var guessNum int

	var guessSlice [4]int
	fmt.Println("开始获取随机数:")
	for {
		num = rand.Intn(10000)
		if num >= 1000 {
			break
		}
	}
	//*p=num
	fmt.Println("num=", num)
	slice[0] = num / 1000
	slice[1] = num % 1000 / 100
	slice[2] = num % 100 / 10
	slice[3] = num % 10
	fmt.Println("slice=", slice)
	for {
		fmt.Println("请输入猜的数字:")
		fmt.Scan(&guessNum)
		if guessNum > 999 && guessNum < 10000 {
			guessSlice[0] = guessNum / 1000
			guessSlice[1] = guessNum % 1000 / 100
			guessSlice[2] = guessNum % 100 / 10
			guessSlice[3] = guessNum % 10
			n := 0
			var count int = 0
			for i := 0; i < 4; i++ {
				if guessSlice[i] > slice[i] {
					fmt.Printf("第%d位大了\n", i+1)
				} else if guessSlice[i] < slice[i] {
					fmt.Printf("第%d位小了\n", i+1)
				} else {
					fmt.Printf("第%d位猜对了\n", i+1)
					n++

				}
				fmt.Println("n=", n)
				if n == 4 {
					fmt.Println("恭喜全部猜对,获得满500减30券\n")
					fmt.Printf("一共猜了%d次\n", count)
					count = 0
					break
				}
				count++
			}

		} else {
			fmt.Println("对不起,您输入的数不合法...")
		}

	}

}
